package com.ken.app_manager.dao.impl;

import com.ken.app_manager.base.OnBaseCallback;
import com.ken.app_manager.bean.UserBean;
import com.ken.app_manager.core.JdbcTemplate;
import com.ken.app_manager.core.PreparedStatementCreator;
import com.ken.app_manager.core.RowCallbackHandler;
import com.ken.app_manager.dao.UserDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImplJdbc implements UserDao {
    private static JdbcTemplate jdbcTemplate = new JdbcTemplate();
    @Override
    public void create(UserBean userBean,OnBaseCallback callback) {
        jdbcTemplate.update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                String sql = "insert into user(userid,username) values(?,?)  ";
                PreparedStatement preparedStatement = connection.prepareStatement(sql);

                preparedStatement.setInt(1, userBean.getUserid());
                preparedStatement.setString(2, userBean.getUsername());
                return preparedStatement;
            }
        }, new OnBaseCallback() {
            @Override
            public void onSuccess(String result) {
                callback.onSuccess(result);
            }

            @Override
            public void onError(int code, String message) {
                callback.onError(code,message);
            }
        });
    }

    @Override
    public void delete(UserBean userBean) {

    }

    @Override
    public void update(UserBean userBean) {

    }

    @Override
    public UserBean findByPk(String pk) {

        return null;
    }

    @Override
    public List<UserBean> findByAll() {
        List<UserBean> userBeanList = new ArrayList<>();
        jdbcTemplate.query(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                String sql = "select userid,username from user ";
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                return preparedStatement;
            }
        }, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet rs) throws SQLException {
                System.out.print("用户id：" + rs.getInt("userid"));
                System.out.println("用户名：" + rs.getString("username"));
                UserBean userBean = new UserBean();
                userBean.setUserid(rs.getInt("userid"));
                userBean.setUsername(rs.getString("username"));
                userBeanList.add(userBean);
            }
        }, new OnBaseCallback() {
            @Override
            public void onSuccess(String result) {

            }

            @Override
            public void onError(int code, String message) {

            }
        });
        return userBeanList;
    }
}
